/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dk.rofl.units;

import dk.rofl.graphics.GameTileSet;
import dk.rofl.items.*;
import java.util.ArrayList;
import rofl.Utils;

/**
 *
 * @author menderleit
 */
public final class Player extends GameUnit {

    private int xp;
    private int nextLevelXp;
    private ArrayList<GameItem> items;
    private int gold;

    // Constructor.
    public Player(GameTileSet gts) {
        super(gts, 1);
        gold = 0;
        xp = 0;
        nextLevelXp = 500;
        setBaseStamina(10);
        setBaseStrength(10);
        setPosition(0, 0);
        items = new ArrayList();
        for (int i = 0; i < Armor.armorSlots; i++) {
            setArmorSlot(i, Armor.generateArmor(1, i, Equipment.QUALITY_CRUDE));
        }
        setWeapon(Weapon.generateWeapon(2, Equipment.QUALITY_NORMAL));
        setCurrentHealth(getMaxHealth());
    }

    // Get the items from a GameChest.
    public void getChestReward(GameChest gc) {
        addGold(gc.getGold());
        ArrayList<GameItem> gItems = gc.getItems();
        for (int i = 0; i < gItems.size(); i++) {
            boolean hpAdded = false;
            if (gItems.get(i) instanceof HealthPotion) {
                HealthPotion hp = (HealthPotion) gItems.get(i);
                for (int j = 0; j < items.size(); j++) {
                    if (items.get(j) instanceof HealthPotion) {
                        HealthPotion h = (HealthPotion) items.get(j);
                        if (h.getItemLevel() == hp.getItemLevel()) {
                            h.addPotion();
                            hpAdded = true;
                        }
                    }
                }
            }
            if (!hpAdded) {
                this.items.add(gItems.get(i));
            }
        }
    }

    // Equip an item from the inventory.
    public void equipInventoryItem(int itemSelected) {
        if (itemSelected >= 0 && itemSelected < items.size()) {
            if (items.get(itemSelected).getItemType() == GameItem.TYPE_ARMOR) {
                Armor a = (Armor) items.get(itemSelected);
                int slot = a.getSlot();
                Armor temp = getArmorSlot(slot);
                setArmorSlot(slot, a);
                items.set(itemSelected, temp);
            }
            if (items.get(itemSelected).getItemType() == GameItem.TYPE_WEAPON) {
                Weapon w = (Weapon) items.get(itemSelected);
                Weapon temp = getWeapon();
                setWeapon(w);
                items.set(itemSelected, temp);
            }
            if (items.get(itemSelected).getItemType() == GameItem.TYPE_POTION) {
                HealthPotion h = (HealthPotion) items.get(itemSelected);
                addHealth(h.getHealth());
                if(h.getNumPotions() == 0) {
                    deleteItem(itemSelected);
                }
            }
        }
    }

    // Delete an item from this player's inventory.
    public void deleteItem(int itemSelected) {
        if (itemSelected >= 0 && itemSelected < items.size()) {
            items.remove(itemSelected);
        }
    }

    // Get a list of this player's inventory.
    public ArrayList<GameItem> getItems() {
        return items;
    }

    // Get a list of items that can be used in combat.
    public ArrayList<GameItem> getCombatItems() {
        ArrayList<GameItem> al = new ArrayList();

        for (int i = 0; i < items.size(); i++) {
            GameItem gi = items.get(i);
            if (!(gi instanceof Armor) && !(gi instanceof Weapon)) {
                al.add(gi);
            }
        }
        return al;
    }

    // Remove a used combat item from the inventory
    public void removeUsedCombatItem(GameItem item) {
        int index = 0;
        for (int i = 0; i < items.size(); i++) {
            if (item == items.get(i)) {
                index = i;
            }
        }
        if(item instanceof HealthPotion) {
            HealthPotion hp = (HealthPotion) item;
            if(hp.getNumPotions() == 0) {
                deleteItem(index);
            }
        }
    }

    // Add an item to the inventory.
    public void addItem(GameItem gi) {
        items.add(gi);
    }

    // How much gold does the player have?
    public int getGold() {
        return gold;
    }

    // Give gold to the player.
    public void addGold(int gold) {
        this.gold += gold;
    }

    // Take gold from the player.
    public void removeGold(int gold) {
        this.gold -= gold;
    }

    // Get the current xp of this player.
    public int getXp() {
        return xp;
    }

    // Get the total amount of xp needed to reach the next level.
    public int getNextLevelXp() {
        return nextLevelXp;
    }

    // Add xp to this player's current xp.
    public void addXp(int xp) {
        this.xp += xp;
        if (this.xp >= nextLevelXp) {
            this.xp -= nextLevelXp;
            nextLevel();
        }
    }

    // Increase this player to the next level.
    @Override
    public void nextLevel() {
        super.nextLevel();
        int sta = 5 - Utils.rnd().nextInt(2) + 2;
        addBaseStamina(3 + sta);
        addBaseStrength(3 + (5 - sta));
        setCurrentHealth(getMaxHealth());
        nextLevelXp = nextLevelXp * 2;
    }

    public void setGold(int gold) {
        this.gold = gold;
    }

    public void setItems(ArrayList<GameItem> items) {
        this.items = items;
    }

    public void setNextLevelXp(int nextLevelXp) {
        this.nextLevelXp = nextLevelXp;
    }

    public void setXp(int xp) {
        this.xp = xp;
    }
    
    
    
    public Object[] savePlayer() {
        int saveLevel = getLevel();
        int saveXp = getXp();
        int saveNextXp = getNextLevelXp();
        int saveGold = getGold();
        int saveCurrentHP = getCurrentHealth();
        Weapon saveWeapon = getWeapon();
        Armor[] saveArmor = getArmor();
        ArrayList<GameItem> saveItems = getItems();
        int saveStamina = getBaseStamina();
        int saveStrengt = getBaseStrength();
               
        Object[] data = {saveLevel, saveXp, saveNextXp, saveGold, saveCurrentHP, saveWeapon, saveArmor, saveItems, saveStamina, saveStrengt};
        
        return data;
    }
    
    public void loadPlayer(Object[] data) {
        
        setLevel((Integer) data[0]);
        setXp((Integer) data[1]);
        setNextLevelXp((Integer) data[2]);
        setGold((Integer) data[3]);
        setCurrentHealth((Integer) data[4]);
        setWeapon((Weapon) data[5]);
        setArmor((Armor[]) data[6]);
        setItems((ArrayList<GameItem>) data[7]);
        setBaseStamina((Integer) data[8]);
        setBaseStrength((Integer) data[9]);
    }
}
